package per.locke.xml.parser.book;

import org.xml.sax.SAXException;
import per.locke.xml.parser.Parser;
import per.locke.xml.pojo.Book;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.List;

public class BookParser extends Parser {

    private BookSAXParserHandler hander = null;

    protected void parser(String targetFile) throws ParserConfigurationException, SAXException, IOException {
        SAXParserFactory factory = SAXParserFactory.newInstance();

        this.hander = new BookSAXParserHandler();

        SAXParser parser = factory.newSAXParser();

        parser.parse(targetFile, hander);

    }

    @Override
    public List<Book> getList(String targetFile) throws IOException, SAXException, ParserConfigurationException {

        parser(targetFile);

        return hander.getBookList();
    }

}
